package com.swt.controller.S_controller;

import com.swt.domain.User;
import com.swt.service.S_service.UpdateUserService;
import com.swt.util.TSaveImageUitl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class UpdateUserController {
    /*注入service*/
    @Resource(name = "S_updateUserService")
    private UpdateUserService updateUserService;
    @RequestMapping("/S_updateUser")
    public String updateUser(MultipartFile myFile, User user, HttpServletRequest request){

        /*调用方法保存图片*/
        String fileName = TSaveImageUitl.saveImage(myFile, request);
        /*判断是否为空*/
        if(fileName!=null){
            /*如果为空说明用户没有修改图片*/
            user.setUserImage(fileName);
        }
        System.out.println(user);
        System.out.println("用户来了============");

        User user1 = (User)request.getSession().getAttribute("USER_SESSION_KEY");
        System.out.println("---------"+user.getUserImage()+"---------");

        if(user.getUserImage()==null){
            user.setUserImage(user1.getUserImage());
        }
        user.setUserRegdate(user1.getUserRegdate());
        user.setUserRegmonth(user1.getUserRegmonth());
        user.setUserPassword(user1.getUserPassword());
        /*调用方法更新用户数据*/
        updateUserService.updateUser(user);
        return "redirect:user/person.html";
    }
}
